草庐IT

c++ - 模板类的实例化点

全部标签

c - Ruby C 扩展开发人员的命名约定

在用C为ruby​​编写扩展时,我对遵循正确的命名约定很感兴趣。具体来说,我指的是将_p添加到谓词的函数名称以及为变量添加前缀m代表模块,c代表类等等。例如,如果我们想在C语言中定义一个像下面这样的谓词方法,我们应该在定义该方法的函数中使用_p作为后缀。classMyClassdefawesome?trueendend在C中:staticVALUEmy_extension_my_class_awesome_p(VALUEself){returnQtrue;}voidInit_my_extension(void){VALUEcMyClass=rb_define_class("MyClas

ruby - 在扩展自身的模块中的实例方法中调用单例方法

我自己扩展了Kernel,在实例方法Kernel#abort的定义中,我调用了单例方法Kernel.abort:moduleKernelextendselfdefabortputs"PressENTERtoexit..."getsKernel.abortendendabort当我调用Kernel#abort时,方法定义中的Kernel.abort调用似乎是指原始的Kernel#abort(扩展为Kernel.abort)。Ruby如何知道当我写Kernel.abort时,我指的是原始的abort方法,而不是我刚刚创建的方法?我将如何递归调用我刚刚创建的新abort方法?

ruby - 使用类似 django 的液体 block /继承的 Jekyll 模板

我正在深入了解Jekyll,并希望将其用作通用前端开发平台,但遇到了Liquid模板语言的局限性,特别是它与Django模板的区别。我发现了liquid-inheritancegem,它添加了Django中最重要的Extends和Block语法。这篇博文进一步扩展了gem以适应Jekyll的文件系统:http://www.sameratiani.com/2011/10/22/get-jekyll-working-with-liquid-inheritance.html问题是它似乎没有以与Django完全相同的方式实现block,这实际上使gem变得无用。为了便于理解,我有两个名为par

ruby-on-rails - 对象实例的未定义​​方法 `includes'

我正在使用Ruby2.2.1和Rails4.2构建应用程序。在我的一个View中,我收到了以下消息:N+1QuerydetectedPolitician=>[:account]Addtoyourfinder::includes=>[:account]N+1Querymethodcallstackapp/models/user.rb:19:in`account'app/controllers/home_controller.rb:6:in`index'这是我在家庭Controller中的操作:@account=current_user.account@new_contacts=curre

ruby - 在 Mixins 中初始化实例变量

是否有任何干净的方法来初始化旨在用作Mixin的模块中的实例变量?例如,我有以下内容:moduleExampledefon(...)@handlers||={}#dosomethingwith@handlersenddefall(...)@all_handlers||=[]#dosomethingwith@all_handlersenddefunhandled(...)@unhandled||=[]#dosomethingwithunhandledenddefdo_something(..)@handlers||={}@unhandled||=[]@all_handlers||=[]#

ruby - 仅当前类的实例方法列表

我有一个O类的实例o。我想知道o的功能。o.methods会给我很多方法。所以我通常做o.methods-Object.instance_methods。但这并不简洁。我想做类似o.methods-o.class.superclass.instance_methods的事情。也就是说,只有O本身定义的方法。还有其他办法吗? 最佳答案 您可以使用方法Module#instance_methods:o.class.instance_methods(false)警告文档似乎是错误的,它说:Withnoargument,orwithanar

ruby - 有没有一种优雅的方法来测试一个实例方法是否是另一个实例方法的别名?

在单元测试中,我需要测试是否正确定义了alias_method定义的别名方法。我可以简单地对用于其原件的别名使用相同的测试,但我想知道是否有更明确或更有效的解决方案。例如,有没有办法1)取消引用方法别名并返回其原始名称,2)获取并比较某种底层方法标识符或地址,或3)获取并比较方法定义?例如:classMyClassdeffoo#dosomethingendalias_method:bar,:fooenddescribeMyClassdoit"methodbarshouldbeanaliasformethodfoo"dom=MyClass.new#???identity(m.bar).s

Ruby:有没有办法获取类的封闭模块常量?

我正在用Ruby进行一些元编程,我需要在模块内动态生成一个同级类。这样做时,我想在模块上调用const_set,但直到运行时我才知道要调用哪个模块常量。一个例子:给定的类Foo::Bar::BazFoo::Quox::Quack我希望能够调用这样的函数(这里过于简单):defgenerate_fromklassmod=klass.enclosing_module#当使用Baz调用时,我最终想要的是一个定义为的新类Foo::Bar::GeneratedClassName还有嘎嘎,我想要Foo::Quox::GeneratedClassName我知道的唯一方法是拆分klass.name,然

ruby-on-rails - Ruby:我可以在类方法中使用实例方法吗?

我有一个包含此类方法的类:defself.get_event_record(row,participant)event=Event.where(:participant_id=>participant.id,:event_type_code=>row[:event_type],:event_start_date=>self.format_date(row[:event_start_date])).firstevent=Event.new(:participant_id=>participant.id,:event_type_code=>row[:event_type],:event_s

ruby - 将实例方法委托(delegate)给类方法

在Ruby中,假设我有一个类Foo允许我对我的大量Foos进行分类。所有Foos都是绿色和球形的是自然界的基本法则,因此我定义了类方法如下:classFoodefself.colour"green"enddefself.is_spherical?trueendend这让我做Foo.colour#"green"但不是my_foo=Foo.newmy_foo.colour#Error!尽管my_foo显然是绿色的。显然,我可以定义一个调用self.class.colour的实例方法colour,但如果我有很多这样的基本特征,那将变得笨拙。我大概也可以通过定义method_missing来尝